<%--
  Created by IntelliJ IDEA.
  User: liuchangling
  Date: 2021/6/5
  Time: 11:48 下午
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <title>班级首页</title>
</head>
<body style="padding: 50px">
<div>
    <button class="btn btn-primary" onclick="addClass()">新增班级</button>
    <button class="btn btn-primary" id="selectClass">查询班级</button>
    <input type="text" placeholder="请输入班级号" id="findClassNo">
    <input type="text" placeholder="请输入年级号" id="findGradeNo">
</div>
<table class="table table-responsive">
    <thead>
    <tr>
        <th>班级</th>
        <th>班级名称</th>
        <th>年级</th>
        <th>班主任名称</th>
        <th>班级口号</th>
        <th>班级人数</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <c:forEach var="classes" items="${allClass}" varStatus="status">
        <c:if test="${status.index%2==0}">
            <tr class="info">
                <td>${classes.classNo}
                </td>
                <td>${classes.className}
                </td>
                <td>${classes.gradeNo}
                </td>
                <td>${classes.teacherName}
                </td>
                <td>${classes.classSlogan}
                </td>
                <td>${classes.classNum}
                </td>
                <td>
                    <button class="btn btn-primary" onclick="updateItem(this)">编辑</button>
                    <button class="btn btn-primary" onclick="deleteItem(this)">删除</button>
                </td>
            </tr>
        </c:if>
        <c:if test="${status.index%2!=0}">
            <tr class="warning">
                <td>${classes.classNo}
                </td>
                <td>${classes.className}
                </td>
                <td>${classes.gradeNo}
                </td>
                <td>${classes.teacherName}
                </td>
                <td>${classes.classSlogan}
                </td>
                <td>${classes.classNum}
                </td>
                <td>
                    <button class="btn btn-primary" onclick="updateItem(this)">编辑</button>
                    <button class="btn btn-primary" onclick="deleteItem(this)">删除</button>
                </td>
            </tr>
        </c:if>
    </c:forEach>
    </tbody>
</table>
</body>
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script>

    function addClass() {
        window.location.href = "addClass.jsp";
    }


    function updateItem(e) {
        var tr = $(e).parent().parent().get(0);
        var classNo, gradeNo;
        $(tr).each(function () {
            classNo = $(this).find("td:first").text();
        })
        console.log(classNo + "," + gradeNo);
        $.ajax({
                type: "POST",
                url: "/DemoUser/getClass",
                async: false,
                data: {classNo: classNo},
                success: function (data, status) {
                    if (status === "success") {
                        window.location.href = "editClass.jsp";
                    }
                }
            }
        )
    }

    function deleteItem(e) {
        var tr = $(e).parent().parent().get(0);
        var classNo, gradeNo;
        $(tr).each(function () {
            classNo = $(this).find("td:first").text();
        })
        $.ajax({
                type: "POST",
                url: "/DemoUser/deleteClass",
                async: false,
                data: {classNo: classNo},
                success: function (data, status) {
                    if (status === "success") {
                        if ('${isEmpty}' === "true") {
                            $(e).parent().parent().remove();
                        }
                        if ('${isEmpty}' === "false") {
                            alert("班级中还有其余学生，不能删除！");
                            history.go(0);
                        }
                    }
                }
            }
        )

    }


    //查询
    $("#selectClass").click(function () {
        //获取输入框输入的值
        var classNo = $("#findClassNo").val();
        $.ajax({
                type: "POST",
                url: "/DemoUser/getClass",
                async: false,
                data: {classNo: classNo},
                success: function (data, status) {
                    if (status === "success") {
                        window.location.href = "selectClassResult.jsp";
                    }
                }
            }
        )
    })


</script>
</html>
